<?php
namespace Home\Controller;

use Common\Controller\BaseController;
use Common\Model\FireModel;
use Common\Model\StaffModel;

class FireController extends BaseController
{
    public $action = 0;
    public $page_size = 10;
    protected $is_have = 2;

    public function _initialize()
    {
        parent::_initialize();
        self::$_action_block = 3;
        $this->assign('block', self::$_action_block);
        if ($this->role_id == self::ROLE_TEACHER_MGR) {
            $this->is_have = 1;
        }
        $this->assign('is_have',$this->is_have);
    }

    public function index()
    {
        $this->action = 2;
        $fireModel = D('Fire');
        $staff_ids = implode(',',$this->getStaffId($this->staff_id));
//        dump($staff_ids);exit;
        $where = " staff.staff_id in({$staff_ids} )and fire.is_drift=1";
        $count = $fireModel
            ->join('staff on staff.staff_id=fire.staff_id', 'LEFT')
            ->where($where)
            ->count();

        vendor('Page');
        $Page = new \Vendor\Page($count, $this->page_size);

        $field = "fire.*,staff.staff_name ";
        $list = $fireModel->field($field)
            ->order('apply_time DESC')
            ->join('staff on staff.staff_id=fire.staff_id', 'LEFT')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();

        $this->assign('list', $list);

        $this->assign('page', $Page->show());
        $this->assign('type', $this->action);
        $this->assign('now', date('Y-m-d'));
        $this->display('index');
    }


    public function add()
    {
        $this->action = 1;
        $staff = $this->getUserInfo();
        $this->assign('staff', $staff);
        $this->assign('now', date('Y-m-d'));
        $this->assign('type', $this->action);
        $this->display('add');
    }

    public function save($drift = 0)
    {
        $post = I('post.');
        $fireModel = D('Fire');
        $post['is_drift'] = $drift ? 2 : 1;
        $res = $fireModel->addFire($post);
        if (false !== $res['status']) {
            $this->success($res['info'], U('Home/Fire/index'));
        } else {
            $this->error($res['info']);
        }
        exit;
    }

    public function drift()
    {
        $staff_ids = implode(',',$this->getStaffId($this->staff_id));
        $this->action = 3;
        $fireModel = D('Fire');
        $where = " staff.staff_id in({$staff_ids}) and fire.is_drift=2";
        $count = $fireModel
            ->join('staff on staff.staff_id=fire.staff_id', 'LEFT')
            ->where($where)
            ->count();

        vendor('Page');
        $Page = new \Vendor\Page($count, $this->page_size);

        $field = "fire.*,staff.staff_name ";
        $list = $fireModel->field($field)
            ->order('apply_time DESC')
            ->join('staff on staff.staff_id=fire.staff_id', 'LEFT')
            ->where($where)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();

        $this->assign('list', $list);

        $this->assign('page', $Page->show());
        $this->assign('type', $this->action);
        $this->display('drift');
    }

    public function edit(){
        $id = I('get.id', 0, 'intval');
        $this->action = 3;
        $staff_ids = implode(',',$this->getStaffId($this->staff_id));
        $where = " fire.id={$id} and staff.staff_id in({$staff_ids}) and fire.is_drift=2";
        $fireModel = D('Fire');
        $field = " fire.*,staff.*";
        $info = $fireModel
            ->field($field)
            ->join('staff on fire.staff_id=staff.staff_id')
            ->where($where)
            ->select();
        if (empty($info)) {
            $this->error('数据不存在');
        }
        $this->assign('info', $info[0]);
        $this->assign('type', $this->action);
        $this->display('edit');
    }

    public function delete(){
        $this->display('delete');
    }

    public function getUserInfo(){
        $staff_id = $this->staff_id;
        // $field = "";
        $staffModel = D('Staff');
        $staff = $staffModel->getStaffById($staff_id);
        return $staff;
    }

    public function view(){
        $id = I('get.id', 0, 'intval');
        $type = I('get.type', 0, 'intval');
        $staff_ids = implode(',',$this->getStaffId($this->staff_id));
        $where = " fire.id={$id} and fire.is_drift=1 and fire.staff_id in({$staff_ids}) ";
        $fireModel = D('Fire');
        $field = " fire.*,staff.*";
        $info = $fireModel
            ->field($field)
            ->join('staff on fire.staff_id=staff.staff_id')
            ->where($where)
            ->select();
        //     echo $fireModel->_sql();
        // dump($info);exit;
        if (empty($info)) {
            $this->error('数据不存在');
        }
        $this->assign('info', $info[0]);
        $view = 'view_' . $type;
        $this->display($view);
    }

    public function judge()
    {
        $id = I('post.fire_id', 0, 'intval');
        $status = I('post.status', 1, 'intval');
        $remark = I('post.remark', '', 'htmlspecialchars');
        $fireModel = D('Fire');
        $res = $fireModel->where("id={$id}")->select();

        if (empty($res)) {
            $this->error('数据不存在');
        }

        $data = array(
            'id' => $id,
            'status' => $status,
            'remark' => $remark
        );

        $res = $fireModel->save($data);
        if (false !== $res) {
            $this->success('操作成功', U('Admin/Fire/index'));
        } else {
            $this->error('操作失败');
        }
    }
    public function getStaffId($staff_id){
        $staffModel = new StaffModel();
        if($this->role_id == self::ROLE_TEACHER_MGR){
            $staff_ids = $staffModel->getStaffByTmgrId($staff_id,'staff_id');
            if(!empty($staff_ids)){
                return array_merge(array_column($staff_ids,'staff_id'),array($staff_id));
            }
        }
        return array($staff_id);
    }
    public function tongji(){
        $stime = I('post.stime',time(),'strtotime');
        $etime = I('post.etime',time(),'strtotime');
        $staff_ids = implode(',',$this->getStaffId($this->staff_id));
//        $where = " staff_id in({$staff_ids}) and TIME_TO_SEC(apply_time)>=".date('Y-m-d',$stime)." and apply_time<=".date('Y-m-d',$etime)." ";
        $where = " staff_id in({$staff_ids}) and is_apply=1 and UNIX_TIMESTAMP(apply_time)>=".$stime." and UNIX_TIMESTAMP(apply_time)<=".$etime." ";
        $fields = " sum(powder_amount) as powder_amoun,sum(powder_amount) as powder_amoun,sum(carbon_amount) as carbon_amount,sum(extinguisher_amount) as extinguisher_amount,sum(blanket_amount) as blanket_amount ";
        $sql = "select {$fields} from fire where {$where}";
        $fireModel = new FireModel();
        $data = $fireModel->query($sql);
        $this->assign('data',$data);
        $this->assign('stime',date('Y-m-d',$stime));
        $this->assign('etime',date('Y-m-d',$etime));
        $this->display('tongji');
    }

}